package com.wc.alorithm_blue_bridge._数据结构.并查集.Q1136;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

/**
 * @Author congge
 * @Date 2023/11/1 15:03
 * @description
 */
public class Main {
    static int[] circle;

    public static void main(String[] args) {
        Input sc = new Input();
        int n = sc.nextInt();
        int m = sc.nextInt();
        circle = new int[2 * n + 1];
        for (int i = 1; i <= 2 * n; i++) {
            circle[i] = i;
        }
        int ans = -1;
        Main main = new Main();
        while (m-- > 0) {
            int idx = sc.nextInt();
            int idy = sc.nextInt();
            if (main.find(idx) == main.find(idy)) {
                ans = idx;
                break;
            } else {
                main.merge(idx, idy + n);
                main.merge(idx + n, idy);
            }
        }
        System.out.println(ans);
    }

    public void merge(int idx, int idy) {
        int preX = find(idx);
        int preY = find(idy);
        circle[preX] = preY;
    }

    public int find(int x) {
        if (circle[x] == x) {
            return x;
        }
        return find(circle[x]);
    }


    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
